﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.ComponentModel;
using MeizuSDK.Drawing;
using System.Diagnostics;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 控件基类（对应于SDK中的UiWin）
    /// </summary>
    public class Control : UIElement
    {
        #region DllImport
        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinDispose(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinEnableNotifyMessage(IntPtr pObj, bool bEnable);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiWinGetPosX(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiWinGetPosY(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiWinGetWidth(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiWinGetHeight(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinSetPos(IntPtr pObj, int x, int y, int w, int h, uint flag);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinSetID(IntPtr pObj, int nID);

        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiWinGetText(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinSetText(IntPtr pObj, [MarshalAs(UnmanagedType.LPWStr)]string text);

        [DllImport("MZFCWrapper.dll")]
        private static extern long UiWinGetTextSize(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern long UiWinGetTextWeight(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinSetTextSize(IntPtr pObj, long nSize);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinSetTextWeight(IntPtr pObj, long nWeight);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiWinCalcContentWidth(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiWinCalcContentHeight(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinEnableScrollBarH(IntPtr pObj, bool bEnable);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinEnableScrollBarV(IntPtr pObj, bool bEnable);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiWinIsEnableScrollBarH(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiWinIsEnableScrollBarV(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinEnableScrollBarOptimize(IntPtr pObj, bool bEnable);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiWinIsEnableScrollOptimize(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinSetVisible(IntPtr pObj, bool bVisible);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiWinIsVisible(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinUpdate(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinInvalidate(IntPtr pObj, ref RECT rectUpdate);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinInvalidate2(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinSetTextColor(IntPtr pObj, uint color);

        [DllImport("MZFCWrapper.dll")]
        private static extern uint UiWinGetTextColor(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinAddChild(IntPtr pObj, IntPtr pChild);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiWinFindChild(IntPtr pObj, IntPtr pChild);

        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiWinGetCapture(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiWinGetChild(IntPtr pObj, int nIndex);

        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiWinGetChildByID(IntPtr pObj, int nID);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiWinGetChildrenCount(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern RECT UiWinGetClientRect(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiWinGetDragMode(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinSetDragMode(IntPtr pObj, int nDragMode);

        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiWinGetParent(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinSetParent(IntPtr pObj, IntPtr pChild);

        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiWinGetParentWnd(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiWinGetScrollableParent(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiWinHasChild(IntPtr pObj, IntPtr pWin);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiWinHasChildNode(IntPtr pObj, IntPtr pWin);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinInsertChild(IntPtr pObj, IntPtr pWin, int nZOrder);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinReleaseCapture(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinSetCapture(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinRemoveAllChildren(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinRemoveChild(IntPtr pObj, IntPtr pChild);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinSetZOrderOfChild(IntPtr pObj, IntPtr pChild, int nZOrder);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinScrollTo(IntPtr pObj, int nScrollToType, int nDstTopPos, bool bAnimate);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinScrollStart(IntPtr pObj, double fScrollSpeed);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinScrollStop(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiWinIsFocused(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiWinSetFocus(IntPtr pObj, bool bFocus);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiWinIsScrolling(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiWinIsScrollable(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiWinIsMouseDownAtScrolling(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiWinIsMouseMoved(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern bool UiWinIsLButtonDown(IntPtr pObj);

        #endregion

        #region 常量、静态变量
        /// <summary>
        /// 默认的字体大小
        /// </summary>
        private const float DefaultFontSize = 20;
        /// <summary>
        /// 默认的字体样式
        /// </summary>
        private const FontStyle DefaultFontStyle = FontStyle.Regular;
        #endregion

        #region 事件
        /// <summary>
        /// 当Parent属性被改变后触发此事件
        /// </summary>
        public event ParentChangedEventHandler ParentChanged;
        /// <summary>
        /// 当控件接收到WM_Paint消息进行重绘时触发此事件
        /// </summary>
        public event PaintEventHandler Paint;
        /// <summary>
        /// 当用户双击控件时触发此事件
        /// </summary>
        public event MouseEventHandler LButtonDoubleClick;
        /// <summary>
        /// 当用户单击控件时触发此事件
        /// </summary>
        public event MouseEventHandler LButtonDown;
        /// <summary>
        /// 当用户抬起鼠标时触发此事件
        /// </summary>
        public event MouseEventHandler LButtonUp;
        /// <summary>
        /// 当鼠标在控件内部移动时触发此事件
        /// </summary>
        public event MouseEventHandler MouseMove;
        #endregion

        #region 私有变量
        /// <summary>
        /// 控件ID
        /// </summary>
        private int nID;
        /// <summary>
        /// 控件父容器
        /// </summary>
        private object parent;
        /// <summary>
        /// 是否启用控件滚动优化
        /// </summary>
        private bool scrollOptimized;
        /// <summary>
        /// 控件可见性
        /// </summary>
        private bool visible = true;
        /// <summary>
        /// 控件左上角X坐标
        /// </summary>
        private int x;
        /// <summary>
        /// 控件左上角Y坐标
        /// </summary>
        private int y;
        /// <summary>
        /// 控件宽度
        /// </summary>
        private int width;
        /// <summary>
        /// 控件高度
        /// </summary>
        private int height;
        /// <summary>
        /// 控件客户区域宽度
        /// </summary>
        private int contentWidth;
        /// <summary>
        /// 控件客户区域高度
        /// </summary>
        private int contentHeight;
        /// <summary>
        /// 是否显示水平滚动条
        /// </summary>
        private bool hScroll;
        /// <summary>
        /// 是否显示垂直滚动条
        /// </summary>
        private bool vScroll;
        /// <summary>
        /// 控件显示的文本
        /// </summary>
        private string text;
        /// <summary>
        /// 字体
        /// </summary>
        private Font font;
        /// <summary>
        /// 控件是否支持自定义绘制
        /// </summary>
        private bool ownerDraw;
        /// <summary>
        /// 是否支持消息通知
        /// </summary>
        private bool notifyMsgEnabled;

        private PaintWinFunc onPaintWin;

        private MouseEventFunc onLButtonDbClk;

        private MouseEventFunc onLButtonDown;

        private MouseEventFunc onLButtonUp;

        private MouseEventFunc onMouseMove;
        /// <summary>
        /// 鼠标最后一次按下的坐标值
        /// </summary>
        private int lastX, lastY;
        /// <summary>
        /// 鼠标左键是否按下
        /// </summary>
        private bool mouseDown;
        #endregion

        #region 受保护属性
        /// <summary>
        /// 控件自绘委托
        /// </summary>
        protected PaintWinFunc PaintWinDelegate
        {
            get
            {
                if (this.onPaintWin == null)
                {
                    this.onPaintWin = new PaintWinFunc(InternalPaintWin);
                }
                return this.onPaintWin;
            }
        }
        /// <summary>
        /// 鼠标左键双击事件委托
        /// </summary>
        protected MouseEventFunc LButtonDbClkDelegate
        {
            get
            {
                if (this.onLButtonDbClk == null)
                {
                    this.onLButtonDbClk = new MouseEventFunc(InternalLButtonDbClk);
                }
                return this.onLButtonDbClk;
            }
        }
        /// <summary>
        /// 鼠标左键按下事件委托
        /// </summary>
        protected MouseEventFunc LButtonDownDelegate
        {
            get
            {
                if (this.onLButtonDown == null)
                {
                    this.onLButtonDown = new MouseEventFunc(InternalLButtonDown);
                }
                return this.onLButtonDown;
            }
        }
        /// <summary>
        /// 鼠标左键抬起事件委托
        /// </summary>
        protected MouseEventFunc LButtonUpDelegate
        {
            get
            {
                if (this.onLButtonUp == null)
                {
                    this.onLButtonUp = new MouseEventFunc(InternalLButtonUp);
                }
                return this.onLButtonUp;
            }
        }
        /// <summary>
        /// 鼠标移动事件委托
        /// </summary>
        protected MouseEventFunc MouseMoveDelegate
        {
            get
            {
                if (this.onMouseMove == null)
                {
                    this.onMouseMove = new MouseEventFunc(InternalMouseMove);
                }
                return this.onMouseMove;
            }
        }
        /// <summary>
        /// 获取或设置一个值，指示鼠标左键是否按下
        /// </summary>
        protected bool MouseDown
        {
            get
            {
                return mouseDown;
            }
            set
            {
                mouseDown = value;
            }
        }
        /// <summary>
        /// 获取或设置最近一次鼠标坐标的X值
        /// </summary>
        protected int LastX
        {
            get
            {
                return lastX;
            }
            set
            {
                this.lastX = value;
            }
        }
        /// <summary>
        /// 获取或设置最近一次鼠标坐标的Y值
        /// </summary>
        protected int LastY
        {
            get
            {
                return lastY;
            }
            set
            {
                this.lastY = value;
            }
        }
        /// <summary>
        /// 获取最后一次鼠标按下的坐标值
        /// </summary>
        protected Point LastMousePosition
        {
            get
            {
                return new Point(LastX, LastY);
            }
        }
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取或设置控件ID
        /// </summary>
        public int Id
        {
            get
            {
                return nID;
            }
            set
            {
                if (value != nID)
                {
                    nID = value;
                    if (IsHandleCreated)
                        UiWinSetID(Handle, value);
                }
            }
        }
        /// <summary>
        /// 获取控件父容器
        /// </summary>
        public object Parent
        {
            get{
                return this.parent;
            }
            set{
                if(value!=this.parent)
                {
                    object prevParent = this.parent;

                    this.parent = value;

                    OnParentChanged(new ParentChangedEventArgs(prevParent));
                }
            }
        }
        /// <summary>
        /// 获取此控件的所有者窗口
        /// </summary>
        public Form OwnerForm
        {
            get
            {
                if (Parent == null)
                    return null;

                if (Parent is Form)
                    return (Form)Parent;
                else
                    return ((Control)Parent).OwnerForm;
            }
        }
        /// <summary>
        /// 获取或设置一个值，指示控件是否启用滚动优化
        /// </summary>
        public bool ScrollOptimize
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinIsEnableScrollOptimize(Handle);
                return scrollOptimized;
            }
            set
            {
                if (value != scrollOptimized)
                {
                    scrollOptimized = value;
                    if (IsHandleCreated)
                        UiWinEnableScrollBarOptimize(Handle, value);
                }
            }
        }
        /// <summary>
        /// 获取或设置控件是否启用鼠标通知
        /// </summary>
        public bool NotifyMessage
        {
            get
            {
                return notifyMsgEnabled;
            }
            set
            {
                if (value != notifyMsgEnabled)
                {
                    notifyMsgEnabled = value;
                    if (IsHandleCreated)
                        UiWinEnableNotifyMessage(Handle, value);
                }
            }
        }
        /// <summary>
        /// 获取或设置控件是否可见
        /// </summary>
        public bool Visible
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinIsVisible(Handle);
                return visible;
            }
            set
            {
                if (value != visible)
                {
                    visible = value;
                    if (IsHandleCreated)
                        UiWinSetVisible(Handle, value);
                }
            }
        }
        /// <summary>
        /// 获取或设置控件在容器中的位置
        /// </summary>
        public override Point Location
        {
            get
            {
                return new Point(X, Y);
            }
            set
            {
                UiWinSetPos(Handle, value.X, value.Y, 0, 0, MzUser.SP_NOSIZE);

                OnLocationChanged(EventArgs.Empty);
            }
        }
        /// <summary>
        /// 获取或设置控件位置的X坐标
        /// </summary>
        public virtual int X
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinGetPosX(Handle);
                return x;
            }
            set
            {
                if (value != x)
                {
                    x = value;
                    if (IsHandleCreated)
                    {
                        UiWinSetPos(Handle, value, Y, 0, 0, MzUser.SP_NOSIZE);

                        OnLocationChanged(EventArgs.Empty);
                    }
                }
            }
        }
        /// <summary>
        /// 获取或设置控件位置的Y坐标
        /// </summary>
        public virtual int Y
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinGetPosY(Handle);
                return y;
            }
            set
            {
                if (value != y)
                {
                    y = value;
                    if (IsHandleCreated)
                    {
                        UiWinSetPos(Handle, X, value, 0, 0, MzUser.SP_NOSIZE);

                        OnLocationChanged(EventArgs.Empty);
                    }

                }
            }
        }
        /// <summary>
        /// 获取或设置控件的大小
        /// </summary>
        public override Size Size
        {
            get
            {
                return new Size(Width, Height);
            }
            set
            {
                if (value.Width != Width || value.Height != Height)
                {
                    Width = value.Width;
                    Height = value.Height;

                    if (IsHandleCreated)
                    {
                        UiWinSetPos(Handle, 0, 0, value.Width, value.Height, MzUser.SP_NOMOVE);

                        OnSizeChanged(EventArgs.Empty);
                    }
                }
            }
        }
        /// <summary>
        /// 获取或设置控件的宽度
        /// </summary>
        public override int Width
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinGetWidth(Handle);
                return width;
            }
            set
            {
                if (value != width)
                {
                    width = value;

                    if (IsHandleCreated)
                    {
                        UiWinSetPos(Handle, 0, 0, value, Height, MzUser.SP_NOMOVE);

                        OnSizeChanged(EventArgs.Empty);
                    }
                }
            }
        }
        /// <summary>
        /// 获取或设置控件的高度
        /// </summary>
        public override int Height
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinGetHeight(Handle);
                return height;
            }
            set
            {
                if (value != height)
                {
                    height = value;
                    if (IsHandleCreated)
                    {
                        UiWinSetPos(Handle, 0, 0, Width, value, MzUser.SP_NOMOVE);

                        OnSizeChanged(EventArgs.Empty);
                    }
                }
            }
        }
        /// <summary>
        /// 获取控件客户区域矩形
        /// </summary>
        public override Rectangle ClientRectangle
        {
            get
            {
                return new Rectangle(X, Y, Width, Height);
            }
        }
        /// <summary>
        /// 获取或设置一个值，指示控件水平滚动条是否可见
        /// </summary>
        public bool HScroll
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinIsEnableScrollBarH(Handle);
                return hScroll;
            }
            set
            {
                if (value != hScroll)
                {
                    hScroll = value;
                    if (IsHandleCreated)
                    {
                        UiWinEnableScrollBarH(Handle, value);
                    }
                }
            }
        }
        /// <summary>
        /// 获取或设置一个值，指示控件垂直滚动条是否可见
        /// </summary>
        public bool VScroll
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinIsEnableScrollBarV(Handle);
                return vScroll;
            }
            set
            {
                if (value != vScroll)
                {
                    vScroll = value;
                    if (IsHandleCreated)
                    {
                        UiWinEnableScrollBarV(Handle, value);
                    }
                }
            }
        }
        /// <summary>
        /// 获取或设置控件文本
        /// </summary>
        public override string Text
        {
            get
            {
                if (IsHandleCreated)
                {
                    IntPtr p = UiWinGetText(Handle);

                    string s = Marshal.PtrToStringUni(p);

                    return s;
                }

                return text;
            }
            set
            {
                if (value != text)
                {
                    text = value;

                    if (IsHandleCreated)
                    {
                        UiWinSetText(Handle, value);
                    }
                }
            }
        }
        /// <summary>
        /// 获取控件的客户区域大小
        /// </summary>
        public override Size ClientSize
        {
            get
            {
                return new Size(ContentWidth, ContentHeight);
            }
        }
        /// <summary>
        /// 获取控件客户区域宽度
        /// </summary>
        protected virtual int ContentWidth
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinCalcContentWidth(Handle);
                return contentWidth;
            }
        }
        /// <summary>
        /// 获取控件客户区域高度
        /// </summary>
        protected virtual int ContentHeight
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinCalcContentHeight(Handle);
                return contentHeight;
            }
        }
        /// <summary>
        /// 获取或设置控件文本颜色
        /// </summary>
        public override Color ForeColor
        {
            get
            {
                uint color = UiWinGetTextColor(Handle);

                return MzDrawHelper.ConvertRgbToColor((int)color);
            }
            set
            {
                int rgb = MzDrawHelper.GetRGB(value);

                UiWinSetTextColor(Handle, (uint)rgb);
            }
        }
        /// <summary>
        /// 获取或设置控件字体
        /// </summary>
        public Font Font
        {
            get
            {
                if (this.font == null)
                {
                    float fontSize = DefaultFontSize;
                    if(IsHandleCreated && !OwnerDraw)
                    {
                        fontSize = UiWinGetTextSize(Handle);
                    }
                    this.font = new Font(FontFamily.GenericSansSerif, fontSize, DefaultFontStyle);
                }
                return this.font;
            }
            set
            {
                if (value != this.font)
                {
                    this.font = value;

                    FontWeight fontWeight = FontWeight.FW_REGULAR;
                    switch (this.font.Style)
                    {
                        case FontStyle.Bold:
                            fontWeight = FontWeight.FW_BOLD;
                            break;
                        default:
                            fontWeight = FontWeight.FW_REGULAR;
                            break;
                    }

                    if (IsHandleCreated && !OwnerDraw)
                    {
                        UiWinSetTextSize(Handle, (long)font.Size);
                        UiWinSetTextWeight(Handle, (long)fontWeight);
                    }

                    Invalidate();
                    Update();
                }
            }
        }
        /// <summary>
        /// 获取一个值，指示控件是否存在焦点
        /// </summary>
        public bool Focused
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinIsFocused(Handle);
                return false;
            }
        }
        /// <summary>
        /// 获取一个值，指示控件是否支持滚动
        /// </summary>
        public bool IsScrollable
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinIsScrollable(Handle);
                return false;
            }
        }
        /// <summary>
        /// 获取一个值，指示左键是否按下
        /// </summary>
        public virtual bool IsLButtonDown
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinIsLButtonDown(Handle);
                return false;
            }
        }
        /// <summary>
        /// 获取一个值，指示左键是否按下且正在滚动
        /// </summary>
        public bool IsMouseDownAtScrolling
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinIsMouseDownAtScrolling(Handle);
                return false;
            }
        }
        /// <summary>
        /// 获取一个值，指示鼠标是否正在移动
        /// </summary>
        public bool IsMouseMoved
        {
            get
            {
                if (IsHandleCreated)
                    return UiWinIsMouseMoved(Handle);
                return false;
            }
        }
        /// <summary>
        /// 获取或设置一个值，指示是否由用户自定义绘制控件
        /// </summary>
        public virtual bool OwnerDraw
        {
            get
            {
                return this.ownerDraw;
            }
            set
            {
                if (value != this.ownerDraw)
                {
                    this.ownerDraw = value;
                    Invalidate();
                    Update();
                }
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造控件对象
        /// </summary>
        internal Control()
        {
            Font = new Font(FontFamily.GenericSansSerif, DefaultFontSize, DefaultFontStyle);
        }
        /// <summary>
        /// 构造控件对象
        /// </summary>
        /// <param name="pObj"></param>
        private Control(IntPtr pObj)
            :this()
        {
            Handle = pObj;
        }
        #endregion

        #region 函数
        /// <summary>
        /// 根据控件指针获取控件实例
        /// </summary>
        /// <param name="pObj"></param>
        /// <returns></returns>
        public static Control FromHandle(IntPtr pObj)
        {
            if (pObj == IntPtr.Zero)
                return null;
            return new Control(pObj);
        }
        /// <summary>
        /// 设置控件是否获得焦点
        /// </summary>
        /// <param name="bFocus">true，表示控件获得焦点；false，表示控件是去焦点</param>
        public void SetFocus(bool bFocus)
        {
            if (IsHandleCreated)
                UiWinSetFocus(Handle, bFocus);
        }
        /// <summary>
        /// 向控件中添加子控件
        /// </summary>
        /// <param name="control"></param>
        internal override void AddChild(Control control)
        {
            if (IsHandleCreated)
                UiWinAddChild(Handle, control.Handle);
        }
        /// <summary>
        /// 移除控件中的子控件
        /// </summary>
        /// <param name="control"></param>
        internal override void RemoveChild(Control control)
        {
            if (IsHandleCreated)
                UiWinRemoveChild(Handle, control.Handle);
        }
        /// <summary>
        /// 移除控件中所有的子控件
        /// </summary>
        internal override void RemoveAllChildren()
        {
            if (IsHandleCreated)
                UiWinRemoveAllChildren(Handle);
        }
        /// <summary>
        /// 更新控件，将无效区域进行重绘
        /// </summary>
        public override void Update()
        {
            if (IsHandleCreated)
                UiWinUpdate(Handle);
        }
        /// <summary>
        /// 使控件区域无效
        /// </summary>
        public override void Invalidate()
        {
            if (IsHandleCreated)
                UiWinInvalidate2(Handle);
        }
        /// <summary>
        /// 使控件指定区域无效，在接收到WM_PAINT消息后将此区域进行更新
        /// </summary>
        /// <param name="rectUpdate"></param>
        public override void Invalidate(Rectangle rectUpdate)
        {
            if (!IsHandleCreated)
                return;

            RECT rect = RECT.FromRectangle(rectUpdate);

            UiWinInvalidate(Handle, ref rect);
        }
        /// <summary>
        /// 设置控件ID
        /// </summary>
        protected void SetID()
        {
            nID = IDGenerator.NewID();
            UiWinSetID(Handle, nID);
        }

        /// <summary>
        /// 开始滚动
        /// </summary>
        /// <param name="scrollSpeed">滚动速度</param>
        public virtual void ScrollStart(double scrollSpeed)
        {
            UiWinScrollStart(Handle, scrollSpeed);
        }
        /// <summary>
        /// 停止滚动
        /// </summary>
        public virtual void ScrollStop()
        {
            UiWinScrollStop(Handle);
        }
        /// <summary>
        /// 滚动控件内容至某个位置
        /// </summary>
        /// <param name="scrollType">滚动类型</param>
        /// <param name="dstTopPos">目的为止。当ScrollType=ScrollToPos时才有效</param>
        /// <param name="animate">是否显示滚动过程</param>
        public virtual void ScrollTo(ScrollType scrollType, int dstTopPos, bool animate)
        {
            UiWinScrollTo(Handle, (int)scrollType, dstTopPos, animate);
        }
        /// <summary>
        /// 将子控件滚动到控件顶部
        /// </summary>
        public void ScrollToTop()
        {
            ScrollTo(ScrollType.ScrollToTop, 0, true);
        }
        /// <summary>
        /// 将子控件滚动到控件底部
        /// </summary>
        public void ScrollToBottom()
        {
            ScrollTo(ScrollType.ScrollToBottom, 0, true);
        }

        /// <summary>
        /// 当控件重绘时调用此函数
        /// </summary>
        /// <param name="hdcDst">控件设备上下文</param>
        /// <param name="prcWin">控件所在矩形</param>
        /// <param name="prcUpdate">控件要更新的矩形区域</param>
        internal void InternalPaintWin(IntPtr hdcDst, ref RECT prcWin, ref RECT prcUpdate)
        {
            if (hdcDst == IntPtr.Zero)
                return;

            if (OwnerDraw)
            {
                using (Graphics g = Graphics.FromHdc(hdcDst))
                {
                    PaintEventArgs e = new PaintEventArgs(g, prcWin.ToRectangle(), prcUpdate.ToRectangle());
                    OnPaintWin(e);
                }
            }
            else
            {
                OnDefaultPaintWin(hdcDst, ref prcWin, ref prcUpdate);
            }
        }
        /// <summary>
        /// 当鼠标在控件中双击左键时，调用此函数
        /// </summary>
        /// <param name="fwKeys">鼠标按键</param>
        /// <param name="xPos">鼠标X坐标</param>
        /// <param name="yPos">Y坐标</param>
        internal int InternalLButtonDbClk(uint fwKeys, int xPos, int yPos)
        {
            MouseEventArgs e = new MouseEventArgs(fwKeys, xPos, yPos);

            OnLButtonDoubleClick(e);

            return 1;
        }
        /// <summary>
        /// 当鼠标在控件中按下左键时，调用此函数
        /// </summary>
        /// <param name="fwKeys">鼠标按键</param>
        /// <param name="xPos">鼠标X坐标</param>
        /// <param name="yPos">Y坐标</param>
        internal int InternalLButtonDown(uint fwKeys, int xPos, int yPos)
        {
            MouseEventArgs e = new MouseEventArgs(fwKeys, xPos, yPos);

            OnLButtonDown(e);

            return 1;
        }
        /// <summary>
        /// 当鼠标在控件中抬起左键时，调用此函数
        /// </summary>
        /// <param name="fwKeys">鼠标按键</param>
        /// <param name="xPos">鼠标X坐标</param>
        /// <param name="yPos">Y坐标</param>
        internal int InternalLButtonUp(uint fwKeys, int xPos, int yPos)
        {
            MouseEventArgs e = new MouseEventArgs(fwKeys, xPos, yPos);

            OnLButtonUp(e);

            return 1;
        }
        /// <summary>
        /// 当鼠标在控件中移动时，调用此函数
        /// </summary>
        /// <param name="fwKeys">鼠标按键</param>
        /// <param name="xPos">鼠标X坐标</param>
        /// <param name="yPos">Y坐标</param>
        internal int InternalMouseMove(uint fwKeys, int xPos, int yPos)
        {
            MouseEventArgs e = new MouseEventArgs(fwKeys, xPos, yPos);

            OnMouseMove(e);

            return 1;
        }
        /// <summary>
        /// 系统绘制控件
        /// </summary>
        /// <param name="hdcDst">控件设备上下文</param>
        /// <param name="prcWin">控件所在矩形区域</param>
        /// <param name="prcUpdate">控件要更新的矩形区域</param>
        protected virtual void OnDefaultPaintWin(IntPtr hdcDst, ref RECT prcWin, ref RECT prcUpdate)
        {
            // 子类继承
        }
 
        /// <summary>
        /// MZFC的命令消息处理函数
        /// </summary>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        internal virtual void OnMzCommand(uint wParam, uint lParam)
        {

        }
        /// <summary>
        /// 控件消息处理函数
        /// </summary>
        /// <param name="message"></param>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        internal uint MzDefWndProc(uint message, uint wParam, uint lParam)
        {
            switch (message)
            {
                case MzUser.MZ_WM_MOUSE_NOTIFY:
                    // 鼠标事件
                    uint notify = (wParam & 0xFFFF0000) >> 16;
                    uint x = lParam & 0xFFFF;
                    uint y = (lParam & 0xFFFF0000) >> 16;
                    // 鼠标单击
                    if (notify == MzUser.MZ_MN_LBUTTONDOWN)
                    {
                        LastX = (int)x;
                        LastY = (int)y;

                        // 触发左键按下事件
                        OnLButtonDown(new MouseEventArgs(0, (int)x, (int)y));
                    }
                    // 鼠标抬起
                    else if (notify == MzUser.MZ_MN_LBUTTONUP)
                    {
                        // 触发左键抬起事件
                        OnLButtonUp(new MouseEventArgs(0, (int)x, (int)y));
                    }
                    // 鼠标移动
                    else if (notify == MzUser.MZ_MN_MOUSEMOVE)
                    {
                        OnMouseMove(new MouseEventArgs(0, (int)x, (int)y));
                    }

                    // 向子控件传递消息
                    int id = (int)(wParam & 0x0000FFFF);
                    Control ctrl = Controls.FindControlById(id);
                    if (ctrl != null)
                    {
                        ctrl.MzDefWndProc(message, wParam, lParam);
                    }
                    break;

                default:
                    break;
            }

            WndProc(message, wParam, lParam);

            return 0;
        }

        #region 事件函数

        /// <summary>
        /// 引发 ParentChanged 事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnParentChanged(ParentChangedEventArgs e)
        {
            if (ParentChanged != null)
            {
                ParentChanged(this, e);
            }
        }
        /// <summary>
        /// 引发 Paint 事件
        /// </summary>
        /// <param name="e">Paint事件参数</param>
        protected virtual void OnPaintWin(PaintEventArgs e)
        {
            if (Paint != null)
            {
                Paint(this, e);
            }
        }
        /// <summary>
        /// 引发 LButtonDoubleClick 事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnLButtonDoubleClick(MouseEventArgs e)
        {
            if (LButtonDoubleClick != null)
            {
                LButtonDoubleClick(this, e);
            }
        }
        /// <summary>
        /// 引发 LButtonDown 事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnLButtonDown(MouseEventArgs e)
        {
            if (LButtonDown != null)
            {
                LButtonDown(this, e);
            }
        }
        /// <summary>
        /// 引发 LButtonUp 事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnLButtonUp(MouseEventArgs e)
        {
            if (LButtonUp != null)
            {
                LButtonUp(this, e);
            }
        }
        /// <summary>
        /// 引发 MouseMove 事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnMouseMove(MouseEventArgs e)
        {
            if (MouseMove != null)
            {
                MouseMove(this, e);
            }
        }
        #endregion

        #region 销毁资源
        /// <summary>
        /// 使控件得以在从内存中释放之前执行最后的清理操作
        /// </summary>
        /// <param name="disposing">为 true 则释放托管资源和非托管资源；为 false 则仅释放非托管资源。</param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (this.font != null)
            {
                this.font.Dispose();
                this.font = null;
            }

            try
            {
                UiWinDispose(Handle);
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
        #endregion

        #endregion
    }
}
